﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FHost1
{
    public partial class FormUploadFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.Cookies.AllKeys.Contains("fhostuser"))
            {
                Response.Redirect("Default.aspx?login=0");
            }
        }

        protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            int userId = Convert.ToInt32(Request.Cookies["fhostuser"].Value.Split('~')[0]);
            int folderId = Convert.ToInt32(Request.Cookies["cfolder"].Value);
            try
            {
                using (FHostDataContext dataContext = new FHostDataContext())
                {
                    if (dataContext.CheckUserFileUploadAllowed(userId).Value)
                    {
                        List<UserInfo> userInfos = dataContext.UserInfos.Where(obj => (obj.UserID == userId)).ToList();
                        decimal fileSize = 0.01M;
                        if (e.FileSize >= 1000)
                        {
                            fileSize = e.FileSize / 1000000;
                        }

                        string fileext = e.FileName.Substring(e.FileName.LastIndexOf('.')).Replace(".", "");
                        string fileName = "";

                        if (e.FileName.Contains("\\"))
                        {
                            fileName = e.FileName.Substring(e.FileName.LastIndexOf('\\')).Replace("\\", "");
                        }
                        else
                        {
                            fileName = e.FileName;
                        }

                        FileUpload fileUpload = new FileUpload()
                        {
                            CreatedDate = DateTime.Now,
                            FileData = e.GetContents(),
                            FileExt = e.ContentType,
                            FileName = fileName,
                            FileSize = fileSize,
                            IsPublic = true,
                            Title = fileName,
                            UserID = userId,
                            FType = dataContext.GetFileType(fileext)
                        };

                        
                        userInfos[0].UploadedSize += fileSize;
                        dataContext.FileUploads.InsertOnSubmit(fileUpload);
                        dataContext.SubmitChanges();

                        dataContext.MapFileFolder(folderId, fileUpload.FileID);
                    }
                }
            }
            catch
            {
                throw;
            }
        }

        protected void btnDone_Click(object sender, EventArgs e)
        {
            Response.Redirect("MyUploads.aspx");
        }
    }
}